package com.example.zyfypt516wh.model;

import static com.example.zyfypt516wh.common.Common.BASEURL;

import com.example.zyfypt516wh.service.UserService;
import com.example.zyfypt516wh.iface.LoginOutIFace;
import com.example.zyfypt516wh.iface.LoginOutListener;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;

public class LoginOutModel implements LoginOutIFace {

    private Retrofit retrofit;

    public LoginOutModel() {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASEURL)

                .addConverterFactory(ScalarsConverterFactory.create())
                .build();
    }

    @Override
    public void getLoginOutResult(String sessionID,LoginOutListener loginOutListener) {

        //使用 Retrofit----2
        UserService userService
                = retrofit.create(UserService.class);
        Call<String> call
                = userService.logout(sessionID);
        //使用 Retrofit----3

        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()  ) {
                    loginOutListener.onResponse(response.body().toString());//登录成功
                    System.out.println("--注销注销成功： " + response.body());

                }  else {
                    loginOutListener.onFail("未知错误");//未知错误
                    System.out.println("--未知错误");
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                loginOutListener.onFail(t.getMessage());
                //在 logcat 中查看错误原因 sout
                System.out.println("--注销错误： " + t.getMessage());
            }
        });
    }
}
